import { request } from '../../request/index.js'

Page({
  data: {
    title: "",
    tagArray: ['标签1', '标签2', '标签3', '标签4', '标签5', '标签6', '标签7'],//物品标签
    tagIndex: 0,
    tagChoosen: '',
    description: "",
    imgs: [], //图片路径
    imgNum: 0, //图片数量,
    price: 0,
    originalPrice: 0,
    // tradeMethodArray: ['自提', '送货'],//交易方式
    tradeMethodIndex: 0,
  },
  onLoad() {
    this.init()
  },
  async init() {
    await this.getTagList()
  },
  // 获取标签列表
  async getTagList() {
    const result = await request({
      url: '/secondhand/goods/getAllLabel',
    })
    this.setData({
      tagArray: result.data.obj
    })
  },
  // 添加标题
  addTitle(e) {
    this.setData({
      title: e.detail.value,
    })
  },
  // 添加描述
  addDescription(e) {
    this.setData({
      description: e.detail.value,
    })
  },
  // 改变标签
  bindTagChange(e) {
    this.setData({
      tagIndex: e.detail.value
    })
  },
  // 标签下标转标签字符串
  tagToString() {
    let n = this.data.tagIndex
    this.setData({ tagChoosen: this.data.tagArray[n] })
  },
  // 选择图片
  chooseImg() {
    let addImgs = this.data.imgs;
    let num = addImgs.length;
    let that = this;
    wx.chooseImage({
      count: 9 - num,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        addImgs = addImgs.concat(res.tempFilePaths);
        num = addImgs.length;
        that.setData({
          imgs: addImgs,
          imgNum: num
        });
      }
    })
  },
  // 添加价格
  addprice(e) {
    this.setData({
      price: e.detail.value,
    })
  },
  // 添加原价
  addPreprice(e) {
    this.setData({
      originalPrice: e.detail.value,
    })
  },
  // 上传
  async post() {
    this.tagToString()
    wx.uploadFile({
      url: '/secondhand/goods/upgoods',
      filePath: this.data.imgs[0],
      name: 'images',
      formData: {
        "goodName": this.data.title,
        "description": this.data.description,
        "labels": this.data.tagChoosen,
        "price": this.data.price,
        "originalPrice": this.data.originalPrice
      },
      success(res) {
        // const data = res.data
        console.log(res)
      }
    })
    console.log(this.data, parseInt(this.data.price))
    // let postInfo={
    //   goodName:this.data.title,
    //   description:this.data.description,
    //   labels:this.data.tagChoosen,
    //   price:parseInt(this.data.price),
    //   originalPrice:parseInt(this.data.originalPrice),
    //   images:this.data.imgs
    // }
    // console.log("1",postInfo)
    // const result = await request({
    //   url: 'http://47.102.99.215:9090/secondhand/goods/upgoods',
    //   header: { 'content-type':'multipart/form-data; boundary=something',authorization: wx.getStorageSync('token') },
    //   method:'POST',
    //   data : postInfo
    // })
    // console.log(result)
  }
})